using System;
using System.Collections.Generic;
using System.Text;
using System.Resources;

namespace PastCrew.Games.Chess.Engine.Entities.Pieces
{
    public abstract class BasePiece
    {        
        private Entities.Color _color;
        private Entities.Board.Square _location;
        private bool _inGame = true;
        private List<Board.Position> _positionHistory = new List<Board.Position>();
        private bool _hasMoved = false;
        
        public Board.Position GetPreviousPosition()
        {
            Board.Position previousPosition = _positionHistory[_positionHistory.Count - 1];
            return previousPosition;
        }

        public bool HasMoved
        {
            get { return this._hasMoved; }
            set { this._hasMoved = value; }
        }

        public bool InGame
        {
            get { return _inGame; }
            set { this._inGame = value; }
        }

        public Entities.Board.Square CurrentSquare
        {
            get {
                if (!this._inGame)
                    throw new ApplicationException(Properties.Resources.ex_pieceNotInGame);
                else
                    return this._location; 
            }
            set {
                if (!this._inGame)
                    throw new ApplicationException(Properties.Resources.ex_pieceNotInGame);
                else
                {
                    if (this.CurrentSquare != null)
                    {
                        this._positionHistory.Add(this.CurrentSquare.Position);                        
                    }
                    this._location = value;
                }

                this._hasMoved = true;
            }
        }

        public Entities.Color Color
        {
            get { return _color; }
            set { this._color = value; }
        }

        public virtual Entities.MoveResult ValidateMove(Entities.Board.Position newPosition)
        {
            Entities.MoveResult result = MoveResult.invalid;

            List<Entities.SquareStatus> validMoves = GetValidMoves();

            foreach (SquareStatus squareStat in validMoves)
            {
                if (squareStat.Square.GetHashCode() == (Board.GetSquare(newPosition)).GetHashCode())
                {
                    result = squareStat.Result;
                }
            }

            return result;
        }

        public abstract List<Entities.SquareStatus> GetValidMoves();
        
    }
}
